package com.rchat.pocmini.activity.groupsselect.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aso.common.utils.ToastUtils;
import com.rchat.pocmini.R;

import java.util.ArrayList;

/**
 * Created by itsdf07 on 2017/8/31.
 */

public class GroupsSelectorListView extends RecyclerView {
    private ArrayList<GroupSelectBean> mData;
    private ArrayList<GroupSelectBean> mSelectData;
    private GroupsSelectAdapter mAdapter;
    private Context mContext;

    public GroupsSelectorListView(Context context) {
        this(context, null);
    }

    public GroupsSelectorListView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GroupsSelectorListView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        setLayoutManager(new LinearLayoutManager(context));
        init();
    }

    private void init() {
        mData = new ArrayList<>();
        mSelectData = new ArrayList<>();
        mAdapter = new GroupsSelectAdapter();
        setAdapter(mAdapter);
    }

    public ArrayList<GroupSelectBean> getSelectData() {
        return mSelectData;
    }

    public void setData(ArrayList<GroupSelectBean> data) {
        if (null == mData) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        mData.addAll(data);
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    private class GroupsSelectAdapter extends RecyclerView.Adapter<GroupsSelectAdapter.GroupsSelectHolder> {

        @Override
        public GroupsSelectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_groupsselector, parent, false);
            return new GroupsSelectHolder(view);
        }

        @Override
        public void onBindViewHolder(final GroupsSelectHolder holder, final int position) {
            final GroupSelectBean bean = mData.get(position);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (bean.isSelectStatus()) {
                        mSelectData.remove(bean);
                    } else {
                        if (mSelectData.size() >= 5) {
                            ToastUtils.showToast(mContext, "您已选择了5个群组，不能再添加了");
                            return;
                        }
                        mSelectData.add(bean);
                    }
                    bean.setSelectStatus(!bean.isSelectStatus());

                    notifyItemChanged(position);
                }
            });
            holder.tvGroupName.setText(bean.getGroupName());
            holder.tvGroupId.setText("ID:" + bean.getGroupId());
            holder.itemView.setBackgroundColor(bean.isSelectStatus() ?
                    getResources().getColor(R.color.color_dddddd) : getResources().getColor(R.color.transparent));

        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        class GroupsSelectHolder extends RecyclerView.ViewHolder {
            /**
             * 群组名称
             */
            private TextView tvGroupName;
            /**
             * 群组ID
             */
            private TextView tvGroupId;


            public GroupsSelectHolder(View itemView) {
                super(itemView);
                tvGroupName = (TextView) itemView.findViewById(R.id.tv_groupName);
                tvGroupId = (TextView) itemView.findViewById(R.id.tv_groupId);
            }
        }
    }
}
